﻿using IRUM.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IRUM.UI
{
    public partial class FormAnalysisDetails : Form
    {
        Dictionary<int, string> dicYearMatrix = new Dictionary<int, string>();
        Dictionary<int, string> dicMonthMatrix = new Dictionary<int, string>();

        public FormAnalysisDetails()
        {
            InitializeComponent();

            MakeYearMatrix();
            MakeMonthMatrix();
        }

        /// <summary>
        /// 종목코드의 년도별 코드를 준비한다.
        /// </summary>
        private void MakeYearMatrix()
        {
            dicYearMatrix.Add(1996, "6");
            dicYearMatrix.Add(1997, "7");
            dicYearMatrix.Add(1998, "8");
            dicYearMatrix.Add(1999, "9");
            dicYearMatrix.Add(2000, "0");
            dicYearMatrix.Add(2001, "1");
            dicYearMatrix.Add(2002, "2");
            dicYearMatrix.Add(2003, "3");
            dicYearMatrix.Add(2004, "4");
            dicYearMatrix.Add(2005, "5");
            dicYearMatrix.Add(2006, "A");
            dicYearMatrix.Add(2007, "B");
            dicYearMatrix.Add(2008, "C");
            dicYearMatrix.Add(2009, "D");
            dicYearMatrix.Add(2010, "E");
            dicYearMatrix.Add(2011, "F");
            dicYearMatrix.Add(2012, "G");
            dicYearMatrix.Add(2013, "H");
            dicYearMatrix.Add(2014, "J");
            dicYearMatrix.Add(2015, "K");
            dicYearMatrix.Add(2016, "L");
            dicYearMatrix.Add(2017, "M");
            dicYearMatrix.Add(2018, "N");
            dicYearMatrix.Add(2019, "P");
            dicYearMatrix.Add(2020, "Q");
            dicYearMatrix.Add(2021, "R");
            dicYearMatrix.Add(2022, "S");
            dicYearMatrix.Add(2023, "T");
            dicYearMatrix.Add(2024, "V");
            dicYearMatrix.Add(2025, "W");
            dicYearMatrix.Add(2026, "6");
            dicYearMatrix.Add(2027, "7");
            dicYearMatrix.Add(2028, "8");
            dicYearMatrix.Add(2029, "9");
            dicYearMatrix.Add(2030, "0");
            dicYearMatrix.Add(2031, "1");
            dicYearMatrix.Add(2032, "2");
            dicYearMatrix.Add(2033, "3");
            dicYearMatrix.Add(2034, "4");
            dicYearMatrix.Add(2035, "5");
        }

        /// <summary>
        /// 종목코드의 월별 코드를 준비한다.
        /// </summary>
        private void MakeMonthMatrix()
        {
            dicMonthMatrix.Add(1, "1");
            dicMonthMatrix.Add(2, "2");
            dicMonthMatrix.Add(3, "3");
            dicMonthMatrix.Add(4, "4");
            dicMonthMatrix.Add(5, "5");
            dicMonthMatrix.Add(6, "6");
            dicMonthMatrix.Add(7, "7");
            dicMonthMatrix.Add(8, "8");
            dicMonthMatrix.Add(9, "9");
            dicMonthMatrix.Add(10, "A");
            dicMonthMatrix.Add(11, "B");
            dicMonthMatrix.Add(12, "C");
        }

        private void FormAnalysisDetails_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Value = new DateTime(2014, 4, 22);
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            if (dateTimePicker1.Value.DayOfWeek == DayOfWeek.Monday)
            {
                dateTimePicker1.Value = dateTimePicker1.Value.AddDays(-3);
            }
            else
            {
                dateTimePicker1.Value = dateTimePicker1.Value.AddDays(-1);
            }
        }

        private void btnAfter_Click(object sender, EventArgs e)
        {
            if (dateTimePicker1.Value.DayOfWeek == DayOfWeek.Friday)
            {
                dateTimePicker1.Value = dateTimePicker1.Value.AddDays(3);
            }
            else
            {
                dateTimePicker1.Value = dateTimePicker1.Value.AddDays(1);
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            string date = dateTimePicker1.Value.ToString("yyyyMMdd");
            DateTime analysisDate = IRUM_Utils.ConvertStringToDateTime(date);
            DateTime closeDate = IRUM_Utils.GetClosingDateOfOption(analysisDate);
            string code = dicYearMatrix[closeDate.Year] + dicMonthMatrix[closeDate.Month];

            //전광판 정보 받아오기
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("date", date);
            param.Add("code", code);

            DataSet ds = DataStore.Instance.ProcedureToDataSet("sp_OptionBoard_s", param, false);

            if (ds.Tables[0].Rows.Count > 0)
            {
                //기준가찾기
                IRUMDataClassesDataContext dc = new IRUMDataClassesDataContext();
                List<double> lstKP200Close = dc.ChartDataMarkets.Where(p => p.shcode.Equals("101") && p.gubun.Equals("D") && p.date.Equals(date)).Select(p => p.close).ToList();
                float OriginPrice = IRUM_Utils.FindOriginPrice(lstKP200Close[0]);

                //만기까지 몇주차인지 찾기
                int days = (closeDate - analysisDate).Days + 1;
                int nWeeks = days / 7;


                //전광판 데이터 셋
                iruM_OptionBoard1.SetData(ds, OriginPrice, nWeeks);
            }
        }
    }
}
